package com.jxd.project.report.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.jxd.project.report.model.Report;
import com.jxd.project.report.service.IReportService;

import com.jxd.project.report.vo.ReportWithEname;
import com.jxd.project.travel.common.Result;
import com.jxd.project.travel.util.FileDeleteUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName ReportController
 * @Description TODO
 * @Author 孙福桐
 * @Date 2025/9/30 15:32
 * @Version 1.0
 */
@RestController
public class ReportController {
    @Autowired
    private IReportService reportService;
    @Autowired
    private FileDeleteUtil fileDeleteUtil; // 图片删除工具

    // 从配置文件读取上传路径和访问前缀
    @Value("${file.upload-path}")
    private String uploadPath;
    @Value("${file.access-path}")
    private String accessPath;



    /**
     * 单个删除图片（供前端调用）
     * @param param 包含imageUrl字段的Map
     */
    @PostMapping("/deletePicture")
    public Result deleteTicketImage(@RequestBody Map<String, String> param) {
        String imageUrl = param.get("imageUrl");
        if (imageUrl == null || imageUrl.trim().isEmpty()) {
            return Result.error("图片URL不能为空");
        }

        boolean isDeleted = fileDeleteUtil.deleteImageByUrl(imageUrl);
        return isDeleted ? Result.success("图片删除成功") : Result.error("图片删除失败");
    }


    @RequestMapping("/getLostDate")
    public Map<String, Object> getLostDate(@RequestBody Map<String, String> queryMap) {
        return reportService.getLostReport(queryMap);
    }

    @RequestMapping("/getLostDateByDept")
    public Map<String, Object> getLostDateByDept(@RequestBody Map<String, String> queryMap) {
        return reportService.getLostReportByDept(queryMap);
    }

    @RequestMapping("/getAllReport")
    public Map<String, Object> getAllReport(@RequestBody Map<String, String> queryMap) {
        return reportService.getAllReport(queryMap);
    }

    @RequestMapping("/editReport")
    public String editDate(@RequestBody Report report) {
        if (reportService.updateReport(report)) return "success";
        else return "fail";
    }

    @RequestMapping("/passBatch")
    public String passBatch(@RequestBody List<Integer> reportids) {
        if (reportService.passBatch(reportids)) return "success";
        else return "fail";
    }

    @RequestMapping("/rejectBatch")
    public String rejectBatch(@RequestBody List<Integer> reportids) {
        if (reportService.rejectBatch(reportids)) return "success";
        else return "fail";
    }

    @RequestMapping("/addReport")
    public String addReport(@RequestBody Report report) {
        if (reportService.saveReport(report)) return "success";
        else return "fail";
    }

    @RequestMapping("/getCheckReport")
    public Map<String, Object> getCheckReport(@RequestBody Map<String, String> queryMap) {
        return reportService.getCheckReport(queryMap);
    }

    @RequestMapping("/getOneReport")
    public ReportWithEname getOneReport(Integer reportid) {
        return reportService.getOneReport(reportid);
    }

    @RequestMapping("/isReport")
    public String isReport(int empno){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dateTime = LocalDateTime.now();
        String formattedDateTime = dateTime.format(formatter);

        QueryWrapper<Report> qw = new QueryWrapper<>();
        qw.and(i->i.eq("empno",empno).eq("reportdate",formattedDateTime));
        if(reportService.getOne(qw) == null) return "false";
        else return "true";
    }

    @RequestMapping("/getReturnReport")
    public Map<String, Object> getReturnReport(@RequestBody Map<String, String> queryMap) {
        return reportService.getReturnReport(queryMap);
    }

    @RequestMapping("/getWorkloadByMonth")
    public Map<String, Object> getWorkloadByMonth(@RequestBody Map<String, String> queryMap) {
        return reportService.getWorkloadByMonth(queryMap);
    }

    @RequestMapping("/getOvertimeByMonth")
    public Map<String, Object> getOverloadByMonth(@RequestBody Map<String, String> queryMap) {
        return reportService.getOvertimeByMonth(queryMap);
    }

    @RequestMapping("/isAbsence")
    public String isAbsence(@RequestBody Map<String, String> queryMap){
        if (reportService.isAbsence(queryMap)) return "success";
        else return "fail";
    }

    @RequestMapping("/getReturnReportNum")
    public String getReturnReportNum(int empno){
        QueryWrapper<Report> qw = new QueryWrapper<>();
        qw.eq("empno",empno).eq("state",0).isNotNull("checkdetail");
        long count = reportService.count(qw);
        return String.valueOf(count);
    }
}
